/******
    ************************************************************************
    ******
    ** @project : XDrive_Step
    ** @brief   : Stepper motor with multi-function interface and closed loop function. 
    ** @brief   : 具有多功能接口和闭环功能的步进电机
    ** @author  : unlir (知不知啊)
    ** @contacts: QQ.1354077136
    ******
    ** @address : https://github.com/unlir/XDrive
    ******
    ************************************************************************
    ******
    ** {Stepper motor with multi-function interface and closed loop function.}
    ** Copyright (c) {2020}  {unlir(知不知啊)}
    ** 
    ** This program is free software: you can redistribute it and/or modify
    ** it under the terms of the GNU General Public License as published by
    ** the Free Software Foundation, either version 3 of the License, or
    ** (at your option) any later version.
    ** 
    ** This program is distributed in the hope that it will be useful,
    ** but WITHOUT ANY WARRANTY; without even the implied warranty of
    ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    ** GNU General Public License for more details.
    ** 
    ** You should have received a copy of the GNU General Public License
    ** along with this program.  If not, see <http://www.gnu.org/licenses/>.
    ******
    ************************************************************************
******/

/*****
  ** @file     : control_config.c/h
  ** @brief    : 控制配置
  ** @versions : 2.2.3
  ** @time     : 2020/09/15
  ** @reviser  : unli (HeFei China)
  ** @explain  : null
*****/

#ifndef CONTROL_CONFIG_H
#define CONTROL_CONFIG_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include <stdbool.h>

typedef struct {
    // 电流配置
    int16_t Rated_Current;        // 额定电流(mA)
    int16_t Cali_Current;         // 校准电流(mA)
    
    // 运动参数
    // int32_t Step_NUM;             // 单圈步数
    int16_t Divide_NUM;           // 细分数
    int16_t Rated_Speed;            //额定转速(r/s)               = Move_Rated_Speed * Move_Pulse_NUM(Move_Step_NUM * Move_Divide_NUM)
    int16_t Rated_UpAcc;            //固件额定加速加速度(r/ss)  = Move_Rated_UpAcc * Move_Pulse_NUM
    int16_t Rated_DownAcc;            //固件额定减速加速度(r/ss)    = Move_Rated_DownAcc * Move_Pulse_NUM
    int16_t Rated_UpCurrentRate;    //固件额定增流梯度(mA/s)    = Move_Rated_UpCurrentRate * Current_Rated_Current
    int16_t Rated_DownCurrentRate; //固件额定减流梯度(mA/s)    = Move_Rated_DownCurrentRate * Current_Rated_Current

} MotorConfig;

extern MotorConfig g_motor_config; // 声明全局配置实例

/********************  硬件配置区  ********************/
#define Current_Rated_Current        getCurrentRatedCurrent()        //额定电流(mA)
#define Current_Cali_Current        getCurrent_Cali_Current()        //校准电流(mA)

/********************  运动参数配置区  ********************/
#define Move_Step_NUM                ((int32_t)(200))                                                                //(使用的电机单圈步数)(每步磁场旋转90°)
#define Move_Divide_NUM            getMoveDivideNum()                                                                //(每步柔性件控制细分量)
#define Move_Pulse_NUM            getMovePulseNum()        //(电机单圈脉冲数)
#define Move_Rated_Speed            getMoveRatedSpeed()                            //(额定转速)(50转每秒)
#define Move_Rated_UpAcc                getMoveRatedUpAcc()                    //(固件额定加速加速度)(1000r/ss)
#define Move_Rated_DownAcc            getMoveRatedDownAcc()                    //(固件额定减速加速度)(1000r/ss)
#define Move_Rated_UpCurrentRate        getMoveRatedUpCurrentRate()    //(固件额定增流梯度)(20倍额定/s)
#define Move_Rated_DownCurrentRate    getMoveRatedDownCurrentRate()    //(固件额定减流梯度)(20倍额定/s)

/****************************************  校准器配置区  ****************************************/
#define CALI_Encode_Bit                    ((int32_t)(14))                                                                //(编码器位宽)(14位输出精度)
#define CALI_Encode_Res                    ((int32_t)((0x00000001U) << CALI_Encode_Bit))    //(编码器分辨率)(2^14 = 16384)(16k分辨率)(占用32k校准空间)
#define CALI_Gather_Encode_Res    ((int32_t)(CALI_Encode_Res / Move_Step_NUM))    //(校准每采集步编码器分辨率)

/****************************************  控制器频率配置区  ****************************************/
#define CONTROL_FREQ_HZ            (20000)                                                //控制频率_hz
#define CONTROL_PERIOD_US        (1000000 / CONTROL_FREQ_HZ)        //控制周期_us

//void Control_Config_Init_Static(void);    //控制静态配置
//void Control_Config_Init_Dynamic(void);    //控制动态配置

int32_t getCurrentRatedCurrent(void);
int32_t getCurrent_Cali_Current(void);
// int32_t getMoveStepNum(void);
int32_t getMoveDivideNum(void);
int32_t getMovePulseNum(void);
int32_t getMoveRatedSpeed(void);
int32_t getMoveRatedUpAcc(void);
int32_t getMoveRatedDownAcc(void);
int32_t getMoveRatedUpCurrentRate(void);
int32_t getMoveRatedDownCurrentRate(void);

#ifdef __cplusplus
}
#endif

#endif
